<link type="text/css" rel="stylesheet" href="css/index.css">

<?php
//开启session
session_start();
/*
 * 连接数据库查询
 */
// 引入PHP文件
require "DBUtil.php";  // 替换引入  引入文件遇到错误会提示并停止执行，
//include "DBUtil.php";  // 执行引入  引入文件遇到错误会提示并继续执行
$sql = "select message.*,userinfo.profile,userinfo.userName,type.type_name from userinfo,type,message where message.uid = userinfo.uid and type.type_code =message.type_code";
$messages = getList($sql,null);
$types = getList("select * from type",null);
?>
<script>
    console.log(`<?php var_dump($messages); ?>`)
    console.log(`<?php var_dump($types); ?>`)
</script>
<?php
if ($_SESSION["userinfo"]):
    $userinfo = $_SESSION["userinfo"];
?>
<div class="head">
    <img src="<?php echo $userinfo['profile'] ?>">
    <div onclick="window.location.href='userinfo.php?uid=<?php echo $userinfo['uid'] ?>'"  class="userinfo">
        <div class="userName"> <?php echo $userinfo['userName'] ?></div>
        <div class="note"> <?php
            echo strlen($userinfo['note'])<120?$userinfo['note']: substr($userinfo['note'],0,122).'...';
        ?>  </div>
    </div>
</div>
<a class="logout" href="logout.php">
    退出登录
</a>
<?php endif; ?>

<?php if (!$_SESSION["userinfo"]): ?>
<div class="head">
    <img src="uploads/head.png">
    <div class="userinfo">
        <div class="userName"> <a href="login.php"> 未登录，去登录 </a> </div>
        <div class="note"> 未登录只能查看，发布信息必须登录! </div>
    </div>
</div>
<?php endif; ?>

<div class="h3">留言板</div>
<div class="add_note">
    <form action="addMessage.php" method="post" >
        <select name="type_code" class="type">
            <?php
                foreach ($types as $type):
                    $n = $type['type_name'];
                    $v = $type['type_code'];
            echo "<option value='$v'>$n</option>";
                endforeach;
            ?>
        </select>
        <div >
            <textarea name="message" class="message" ></textarea>
        </div>
        <input type="hidden" name="uid" value="<?php echo $_SESSION['userinfo']['uid'] ?>">
        <?php if ($_SESSION["userinfo"]): ?>
            <button class="bigBtn" >添加信息 </button>
        <?php else: ?>
            <a class="bigBtn" href="login.php"> 未登录，去登录 </a>
        <?php endif; ?>
    </form>
</div>

<div class="mes_list">
    <?php foreach ($messages as $message): ?>
    <div class="messageInfo">
        <img src="<?php echo $message['profile'] ?>" align="bottom">
        <span ><?php echo $message['userName'] ?> </span>
        <span class="sj"><?php echo $message['createTime'] ?></span>
        <div class="content"><?php echo $message['content'] ?> </div>
        <div class="bkxx">
            <div class="tn"> <?php echo $message['type_name'] ?> </div>
            <?php if($message['uid'] == $_SESSION['userinfo']['uid']):  ?>
            <div class="delMes" > <a href="delMes.php?mid=<?php echo $message['m_id'] ?>"> 删除 </a>   </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endforeach; ?>



</div>
